package com.zc.app.sys.controller;

import com.zc.app.shiro.utils.JsonUtils;
import com.zc.app.sys.dao.peer.SystemMenuPeer;
import com.zc.app.sys.dao.peer.SystemRoleMenuPeer;
import com.zc.app.sys.dao.peer.SystemRolePeer;
import com.zc.app.sys.dao.peer.SystemUserPeer;
import com.zc.app.sys.service.SystemMenuService;
import com.zc.app.sys.service.SystemRoleMenuService;
import com.zc.app.sys.service.SystemRoleService;
import com.zc.framework.common.bean.AjaxResult;
import com.zc.framework.common.bean.MyPageInfo;
import com.zc.framework.system.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by zc on 2016/12/19.
 */
@Controller
@RequestMapping("/sys/role/menu")
public class SystemRoleMenuController extends BaseController  {

    private static Logger logger = LogManager.getLogger(SystemRoleMenuController.class);

    @Autowired
    private SystemRoleMenuService systemRoleMenuService;

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private SystemMenuService systemMenuService;

    @InitBinder({ "sysRoleMenuQuery" })
    public void initBinderQuery(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysRoleMenuQuery.");
    }

    @InitBinder({ "sysRoleMenu" })
    public void initBinderSysRoleMenu(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sysRoleMenu.");
    }



    @RequestMapping(value = "/role_menu", method = {RequestMethod.GET})
    public ModelAndView index(SystemRoleMenuPeer roleMenuPeer){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/role_menu");
        return modelAndView;
    }

    @RequestMapping(value = "/edit", method = {RequestMethod.GET})
    public ModelAndView edit(@RequestParam(value = "pageType",required = false)String pageType,
                             @RequestParam(value = "roleMenuId",required = false)Integer roleMenuId){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/role_menu_edit");
        SystemRoleMenuPeer roleMenuPeer = new SystemRoleMenuPeer();
        if("edit".equals(pageType)
                && roleMenuId!=null){
            roleMenuPeer = systemRoleMenuService.selectByPrimaryKey(roleMenuId);
        }
        modelAndView.addObject("sysRoleMenu", roleMenuPeer);
        modelAndView.addObject("pageType",pageType);
        return modelAndView;
    }

    @RequestMapping(value = "/view", method = {RequestMethod.GET})
    public ModelAndView view(@RequestParam(value = "pageType",required = false)String pageType,
                             @RequestParam(value = "roleMenuId",required = false)Integer roleMenuId){
        ModelAndView modelAndView = new ModelAndView("/admin/sys/role/role_menu_view");
        SystemRoleMenuPeer roleMenuPeer = systemRoleMenuService.selectByPrimaryKey(roleMenuId);
        modelAndView.addObject("sysRoleMenu", roleMenuPeer);
        modelAndView.addObject("pageType",pageType);
        return modelAndView;
    }

    @RequestMapping(value = "/list", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public MyPageInfo<SystemRoleMenuPeer> search(@ModelAttribute("sysRoleMenuQuery")SystemRoleMenuPeer roleMenuPeer) throws InvocationTargetException, IllegalAccessException {
        if(roleMenuPeer==null){
            roleMenuPeer = new SystemRoleMenuPeer();
        }
        MyPageInfo<SystemRoleMenuPeer> pageInfo = systemRoleMenuService.queryPageRoleMenus(roleMenuPeer, roleMenuPeer.getPage(),roleMenuPeer.getRows());
        logger.info("pageInfo:"+pageInfo);
        return pageInfo;
    }

    @RequestMapping(value = "/roles", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<SystemRolePeer> getRoles(){
        return systemRoleService.queryRoles();
    }

    @RequestMapping(value = "/menus", method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public List<SystemMenuPeer> getParentMenu() throws IOException {
        List<SystemMenuPeer> rsList = systemMenuService.getParentMenu();
       return rsList;
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult login(@ModelAttribute("sysRoleMenu") SystemRoleMenuPeer roleMenuPeer,
                            @RequestParam(value = "pageType",required = false)String pageType) {

        if("edit".equals(pageType)){
            if (roleMenuPeer.getRoleMenuId()==null) {
                return fail(false, "ID不能为空!");
            }
            systemRoleMenuService.updateByPrimaryKey(roleMenuPeer);
            return success(true);
        }

        if (roleMenuPeer.getRoleId()==null) {
            return fail(false, "角色不能为空!");
        }
        if (roleMenuPeer.getMenuId()==null) {
            return fail(false, "菜单不能为空!");
        }

        systemRoleMenuService.createRoleMenu(roleMenuPeer);
        return success(true);
    }
}
